package com.jy.api.dao;

import com.jy.api.domain.Store;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Map;

public interface StoreDao extends CrudRepository<Store, String> {

    /**
     * 获取用户对应店铺列表
     *
     * @param userName
     * @param storeIds
     * @return
     */
    List<Map<String, Object>> getStoreList(String userName, List<String> storeIds);


    List<Store> findByUsernameLike(String userName);

    /**
     * 通过邀请码查询店铺信息
     *
     * @param codes
     * @return
     */
    Page<Store> findListByReferralCodeIn(List<String> codes, Pageable pageable);



    List<Store> findByReferralCodeAndEnabledOrderByExpireTimeDesc(String refCode,boolean enabled);


    Page<Store> findAllByEnabledOrderByExpireTimeDesc(boolean enabled,Pageable pageable);

    /**
     * 查询会员相关店铺信息(返回10条信息)
     *
     * @param memberId
     * @param q
     * @return
     */
    @Query(value = "select b.id,b.name,b.addr from member_account a left JOIN store b on(a.store_id=b.id)" +
            " WHERE a.member_id=?1 AND b.name LIKE ?2% limit 10", nativeQuery = true)
    List<Object[]> queryLikeStoreName(String memberId, String q);

    /**
     * 查询会员相关店铺信息(返回10条信息)
     *
     * @param q
     * @return
     */
    @Query(value = " select * from store WHERE name LIKE ?1% limit 10", nativeQuery = true)
    List<Store> queryLikeStoreNameAll(String q);


    List<Store> findByIdIn(List<String> ids);


    /**
     * 未生成客户端地址的店铺
     *
     * @return
     */
    List<Store> findListByClientUrlIsNull();


    List<Store> findListByEnabled(Boolean enabled);

    List<Store> findListByEnabledOrderByCallDesc(Boolean enabled);


    Page<Store> findListByEnabledOrderByCallDesc(Boolean enabled,Pageable pageable);

    Page<Store> findListByEnabledOrderByExpireTimeDesc(Boolean enabled,Pageable pageable);


    @Query(value = " select * from store WHERE name LIKE %?1% AND enabled=1 limit 10", nativeQuery = true)
    List<Store> findByStoreNameLike(String name);


    List<Store> findByMchIdIsNotNull();

}
